//
//  CuoTiLvVC.m
//  SanYiTeaching
//
//  Created by 李文强 on 2019/6/19.
//  Copyright © 2019 SanYi. All rights reserved.
//

#import "CuoTiLvVC.h"
#import "SYRequestAPI+SYXiaoXi.h"
#import "CuoTiLvModel.h"
#import "CuoTiShiTiXiangQingVC.h"
#import "SYRequestAPI+Teach.h"
#import "CuoTiDetailModel.h"
#import "ZXFDCreateClassView.h"

@interface CuoTiLvVC ()
@property (nonatomic, strong) NSMutableArray *dataArray;
@property (nonatomic, strong) CuoTiShiTiXiangQingVC *CTSvc;
@property (nonatomic, strong) ZXFDCreateClassView *createClassView;
@property (copy, nonatomic) NSString *cjClassName;//辅导班级名字
@property (copy, nonatomic) NSString *fdbjId;//辅导班级id
@end

@implementation CuoTiLvVC

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(dismissChuangJianBanJi) name:@"ZaiXianFuDaoTianJiaXueShengView" object:nil];
}
- (void)initSubView{
    _dataArray = [NSMutableArray new];
    self.view.backgroundColor = MAINBACKCOLOR;
    self.navigationController.navigationBar.hidden = YES;
    [self.view addSubview:self.headerView];
    [self.view addSubview:self.tableList];
    [self.view addSubview:self.createClassButton];
    [_tableList headerRefreshBlock:^{
        [self getRequest];
    }];
}
//删除掉
- (void)dismissChuangJianBanJi{
    [UIView animateWithDuration:0.5 animations:^{
        self->_createClassView.frame = CGRectMake(0, self.view.frame.size.width,self.view.frame.size.width,self.view.frame.size.height);
    } completion:^(BOOL finished) {
        [self->_createClassView removeFromSuperview];
    }];
}
- (void)dealloc{
    [[NSNotificationCenter defaultCenter]removeObserver:self name:@"ZaiXianFuDaoTianJiaXueShengView" object:nil];
}

- (void)initOption{
    [_headerView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(@20);
        make.right.equalTo(@0).offset(-20);
        make.top.equalTo(@24);
        make.height.equalTo(@40);
    }];
    [_tableList mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(@20);
        make.right.equalTo(@0).offset(-20);
        make.top.equalTo(self->_headerView.mas_bottom);
        make.bottom.equalTo(@0).offset(-20);
    }];
    [_createClassButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self.view.mas_centerX);
        make.bottom.equalTo(self.view.mas_bottom).offset(-40);
        make.width.equalTo(@80);
        make.height.equalTo(@40);
    }];
}
#pragma mark - 创建班级
- (UIButton *)createClassButton{
    if (!_createClassButton) {
        UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
        [button setTitle:@"创建班级" forState:UIControlStateNormal];
        [button setTitleColor:SELECTEDCOLOR forState:UIControlStateNormal];
        [button setBackgroundColor:[UIColor whiteColor]];
        button.titleLabel.font = [UIFont systemFontOfSize:14];
        button.layer.cornerRadius = 2;
        button.layer.borderWidth = 1;
        button.layer.borderColor = SELECTEDCOLOR.CGColor;
        button.layer.masksToBounds = YES;
        [button addTarget:self action:@selector(createClassClick:) forControlEvents:UIControlEventTouchUpInside];
        _createClassButton = button;
    }
    return _createClassButton;
}
#pragma mark - 前往创建班级
- (void)createClassClick:(UIButton *)sender{
    NSLog(@"前往创建班级");
    _createClassView = [[ZXFDCreateClassView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
    _createClassView.frame = CGRectMake(self.view.frame.size.width, 0, self.view.frame.size.width, self.view.frame.size.height);
    _createClassView.mesgId = self.msgId;
    _createClassView.isMessage = YES;
    _createClassView.fdbjName = self.cjClassName;
    _createClassView.fdbjId = self.fdbjId;
    _createClassView.className = @"考试试题错误率前三";
    _createClassView.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:_createClassView];
    [_createClassView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(@0);
        make.right.equalTo(@0);
        make.top.equalTo(@20);
        make.bottom.equalTo(@0);
    }];
}
- (CuoTiLvHeader *)headerView{
    if (!_headerView) {
        CuoTiLvHeader *view = [[CuoTiLvHeader alloc]initWithFrame:CGRectMake(20, 24, self.view.frame.size.width, 40)];
        [view.closeButton setBackgroundImage:[UIImage imageNamed:@"fudao_close-1"] forState:UIControlStateNormal];
        [view.closeButton addTarget:self action:@selector(closeClick:) forControlEvents:UIControlEventTouchUpInside];
        _headerView = view;
    }
    return _headerView;
}
- (void)closeClick:(UIButton *)sender{
    [self.view removeFromSuperview];
}
- (UITableView *)tableList{
    if (!_tableList) {
        UITableView *tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 144, self.view.frame.size.width-200, APP_HEIGHT-84) style:UITableViewStylePlain];
        tableView.delegate  =self;
        tableView.dataSource = self;
        tableView.backgroundColor = [UIColor whiteColor];
        [tableView registerClass:[CuoTiLvCell class] forCellReuseIdentifier:@"CuoTiLvCell"];
        tableView.scrollEnabled = YES;
        tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _tableList = tableView;
    }
    return _tableList;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _dataArray.count;
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 40;
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return 21;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 40;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    CuoTiLvCell *cell = [tableView dequeueReusableCellWithIdentifier:@"CuoTiLvCell" forIndexPath:indexPath];
    if (!cell) {
        cell = [[CuoTiLvCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"CuoTiLvCell"];
    }
    if (indexPath.row%2 == 0) {
        cell.backgroundColor = [UIColor whiteColor];
    }else{
        cell.backgroundColor = CONTENTBACKCOLOR;
    }
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    CuoTiLvModel *model = _dataArray[indexPath.row];
    [cell reloadViewArray:@[[NSString stringWithFormat:@"%d",(int)indexPath.row+1],model.questionSerial,model.wrongNum,model.rightRate,@""]];
    cell.index = (int)indexPath.row;
    //查看试题图片
    
    cell.block = ^(int index) {
        CuoTiLvModel *mo = self->_dataArray[index];
         [self getZuoTiXueShengRequest:mo andeType:mo.questionType];
    };
   
    return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
//    NSLog(@"点击了cell");
 
    
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    return self.topLabelView;
}
- (SYTopLabView *)topLabelView{
    if (!_topLabelView) {
        SYTopLabView *view = [[SYTopLabView alloc]initWithDataNameArr:@[@"名次",@"大题号",@"错题数",@"错题率",@"操作"] width:0.2];
        view.backgroundColor = [UIColor whiteColor];
        _topLabelView = view;
    }
    return _topLabelView;
}
- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    UIView *view = [[UIView alloc]init];
    view.frame = CGRectMake(0, 0, self.view.frame.size.width, 21);
    return view;
}
- (void)getRequest{
    if (_msgId.length == 0) {
        _msgId = @"62";
    }
    [MBProgressHUD mj_showMessage:nil toView:self.view];
    __weak typeof(self) weakSelf = self;
    [SYRequestAPI sy_getGetQuestionWrongRateInfoClassId:_msgId success:^(NSMutableArray *dataListArr) {
        if (dataListArr.count != 0) {
            CuoTiLvModel *model = dataListArr[0];
            self->_headerView.kaoShiNameLabel.text = [NSString stringWithFormat:@"考试名称:%@  考试班级:%@",model.title,model.className];
            weakSelf.cjClassName = model.className;
            weakSelf.fdbjId = model.classId;
            self->_dataArray = dataListArr;
            [self->_tableList reloadData];
        }
        [MBProgressHUD mj_hideHUDForView:self.view];
    } fail:^(NSError *error) {
        [MBProgressHUD mj_hideHUDForView:self.view];
    }];
}
- (void)getZuoTiXueShengRequest:(CuoTiLvModel *)model andeType:(NSString *)type{
    NSDictionary *parama = @{@"templateQuestionId":model.templateQuestionId,
                             @"studentsIds":model.studentIds};
    [MBProgressHUD mj_showMessage:nil toView:self.view];
    [SYRequestAPI sy_getClassServiceAnsweredQuestionDetail:parama success:^(id  _Nonnull responseObject) {
//        YLSLog(@"输出做题的人 %@", responseObject);
        CuoTiDetailModel *modelOfDetail = [CuoTiDetailModel yy_modelWithJSON:[responseObject objectForKey:@"data"]];
        modelOfDetail.dataStudentsArray = [NSMutableArray new];
        for (NSDictionary *dic in modelOfDetail.dataStudents) {
            DataStudentsModel *m = [DataStudentsModel yy_modelWithJSON:dic];
            [modelOfDetail.dataStudentsArray addObject:m];
        }
        self->_CTSvc = [[CuoTiShiTiXiangQingVC alloc]init];
        self->_CTSvc.mainModel = modelOfDetail;
        self->_CTSvc.questionType = type;
        [self->_CTSvc reloadView];
        [self.view addSubview:self->_CTSvc.view];
        [MBProgressHUD mj_hideHUDForView:self.view];
    } fail:^(NSError *error) {
        [MBProgressHUD mj_hideHUDForView:self.view];
    }];
}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
